package com.lanou.connotationjoke.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.db.HomePictureEntity;
import com.lanou.connotationjoke.db.HomeSubscriptionEntity;
import com.lanou.connotationjoke.entity.CategoryList;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/22.
 */
public class HomeSubscriptionListViewAdapter extends BaseAdapter{
    private Context context;
    private List<HomeSubscriptionEntity> datas;

    public HomeSubscriptionListViewAdapter(Context context, List<HomeSubscriptionEntity> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder = null;
        if (null == convertView){
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_listview_home_subscription,null);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (MyViewHolder) convertView.getTag();
        }
        holder.iconIv.setDefaultImageResId(R.mipmap.ic_launcher);
        holder.iconIv.setErrorImageResId(R.mipmap.ic_launcher);
        holder.iconIv.setImageUrl(datas.get(position).getSmall_icon_url()
                , VolleySingleton.getImageLoader());
        holder.nameTv.setText(datas.get(position).getName());
        holder.introTv.setText(datas.get(position).getIntro());
        return convertView;
    }
    public void refreshData(List<HomeSubscriptionEntity> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }



    class MyViewHolder{
        NetworkImageView iconIv;
        TextView nameTv;
        TextView introTv;

        public MyViewHolder(View itemView) {
            iconIv = (NetworkImageView) itemView.findViewById(R.id.iv_home_subscription);
            nameTv = (TextView) itemView.findViewById(R.id.tv_name_home_subscription);
            introTv = (TextView) itemView.findViewById(R.id.tv_intro_home_subscription);
        }
    }

}
